package org.ehe.erp.service.batch.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.satoken.utils.LoginHelper;
import org.ehe.erp.domain.batch.ErpBatchInfo;
import org.ehe.erp.domain.batch.bo.ErpBatchBo;
import org.ehe.erp.domain.batch.vo.ErpBatchVo;
import org.ehe.erp.mapper.batch.ErpBatchInfoMapper;
import org.ehe.erp.service.batch.ErpBatchInfoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 批次信息管理
 * @author :zhangnn
 * @className :ErpBatchInfoServiceImpl
 * @description: TODO
 * @date 2025-08-18 10:59:36
 */
@Slf4j
@Service
public class ErpBatchInfoServiceImpl extends ServiceImpl<ErpBatchInfoMapper, ErpBatchInfo> implements ErpBatchInfoService {
    /**
     * 分页
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<ErpBatchVo> queryPageList(ErpBatchBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpBatchInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getType()),ErpBatchInfo::getType,bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()),ErpBatchInfo::getTypeName,bo.getTypeName());
        lqw.orderByAsc(ErpBatchInfo::getId);
        IPage<ErpBatchVo> page = this.baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public ErpBatchVo queryById(Long id) {
        return this.baseMapper.selectVoById(id);
    }

    /**
     * 新增
     * @param bo
     */
    @Override
    public boolean insertByBo(ErpBatchBo bo) {
        ErpBatchInfo add = MapstructUtils.convert(bo, ErpBatchInfo.class);
        add.setCreateBy(LoginHelper.getUsername());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改
     * @param bo
     */
    @Override
    public boolean updateByBo(ErpBatchBo bo) {
        ErpBatchInfo update = MapstructUtils.convert(bo, ErpBatchInfo.class);
        update.setUpdateBy(LoginHelper.getUsername());
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            bo.setId(update.getId());
        }
        return flag;
    }

    /**
     * 查询
     * @param typeName
     * @return
     */
    @Override
    public long selectByName(String typeName) {
        return this.baseMapper.selectCount(new QueryWrapper<ErpBatchInfo>().lambda().eq(ErpBatchInfo::getTypeName,typeName));
    }

    /**
     * 删除
     * @param ids
     * @param b
     * @return
     */
    @Override
    public boolean deleteWithValidByIds(List<Long> ids, boolean b) {
        if(b){
            //使用的时候不能进行删除
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
